﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.UserControl
{
    public partial class SuggestTextBox2 : System.Web.UI.UserControl
    {
        private const string Key_SQLCommand = "SQLCommand";
        private const string Key_SQLWhere = "SQLWhere";

        #region 属性
        public Boolean Disabled
        {
            set
            {
                txtName.Enabled = value;
                if (value == true)
                {
                    txtName.Style["background-color"] = "#ececec";
                    txtName.Style["font-color"] = "#000";
                }
                else
                {
                    txtName.Style["background-color"] = "#74E4F1";
                    txtName.Style["font-color"] = "";
                }
            }
        }

        public string Text
        {
            get
            {
                return txtName.Text;
            }
            set
            {
                txtName.Text = value;
            }
        }

        public Unit Width
        {
            set
            {
                txtName.Width = value;
            }
        }

        /// <summary>
        ///  是否是必填项
        /// </summary>
        public bool IsRequired
        {
            set
            {
                if (value == true)
                {
                    spanReq.Visible = true;
                    rfvName.Visible = true;
                }
                else
                {
                    spanReq.Visible = false;
                    rfvName.Visible = false;
                }
            }

        }

        /// <summary>
        ///  ValidationGroup
        /// </summary>
        public string ValidationGroup
        {
            set
            {
                rfvName.ValidationGroup = value;
                rfvValue.ValidationGroup = value;
            }
        }

        /// <summary>
        ///  必添提示信息
        /// </summary>
        public string RequiredErrorMsg
        {
            set
            {
                rfvName.ErrorMessage = value;
            }
        }

        /// <summary>
        /// 输入人员不存在的提示信息
        /// </summary>
        public string NoExitsErrorMsg
        {
            set
            {
                rfvValue.ErrorMessage = value;
            }
        }
        #endregion

        #region Value
        /// <summary>
        ///  Value
        /// </summary>
        public string Value
        {
            get
            {
                return hidValue.Value;
            }
            set
            {
                hidValue.Value = value;
            }
        }
        #endregion

        #region CallBack
        /// <summary>
        ///  Value
        /// </summary>
        public bool CallBack
        {
            set
            {
                if (value)
                {
                    btnHID.Visible = value;
                }
                else
                {
                    btnHID.Visible = false;
                }
            }
        }
        #endregion

        /// <summary>
        /// 数据绑定的SQL命令。格式：（DataTextField,DataValueField,TableName,拼音字段名，第一个字母字段名）
        /// </summary>
        public string SQLCommand
        {
            get
            {
                if (ViewState[Key_SQLCommand] == null)
                {
                    return "";
                }
                else
                {
                    try
                    {
                        return ViewState[Key_SQLCommand].ToString();
                    }
                    catch
                    {
                        return "";
                    }
                }
            }
            set
            {
                ViewState[Key_SQLCommand] = value;
            }
        }


        /// <summary>
        ///  SQL Where
        /// </summary>
        public string SQLWhere
        {
            get
            {
                if (ViewState[Key_SQLWhere] == null)
                {
                    return "";
                }
                else
                {
                    try
                    {
                        return ViewState[Key_SQLWhere].ToString();
                    }
                    catch
                    {
                        return "";
                    }
                }
            }
            set
            {
                ViewState[Key_SQLWhere] = value;
            }
        }

        public Int32 Size
        {
            set { txtName.Width = value; }
        }

        public void refresh()
        {
            Page_Load(null, null);
        }

        public delegate void TextChangeHandle(object sender, EventArgs e);

        public event TextChangeHandle TextChange;

        //public TextChangeHandle TextChange
        //{
        //    set;
        //    get;
        //}

        protected void btnHID_Click(object sender, EventArgs e)
        {
            if (null != TextChange)
            {
                TextChange(sender, e);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            //string paras = "SQL=" + SQLCommand + "&SQLWhere=" + SQLWhere;
            //foreach (Control c in this.Controls)
            //{
            //    if (c is AutoCompleteExtender)
            //    {
            //        AutoCompleteExtender cc = c as AutoCompleteExtender;
            //        cc.ContextKey = paras;
            //        //cc.BehaviorID = Value;
            //    }
            //}
            //Session["SqlWhere" + contextKey] = paras;
            //txtName.Attributes.Add("onkeyup", "AjaxRequestXml2('" + this.ClientID + "','" + paras + "')");
            //txtName.Attributes.Add("onblur", "onBlur2('" + this.ClientID + "')");
            //txtName.Attributes.Add("onkeydown", "AjaxRequestXml2('" + this.ClientID + "','" + paras + "')");
            //}

        }
    }
}
